package com.nice.demo.test;

import android.graphics.Point;

import java.text.DecimalFormat;


public class MathArithmetic {
    public static DecimalFormat sDecimalFormat = new DecimalFormat("#.00");

    public MathArithmetic() {
    }

    public static float lerp(float a, float b, float f) {
        return a + f * (b - a);
    }

    public static double[] rotateVec(float px, float py, double ang, boolean isChLen, double newLen) {
        double[] param = new double[2];
        double vx = (double)px * Math.cos(ang) - (double)py * Math.sin(ang);
        double vy = (double)px * Math.sin(ang) + (double)py * Math.cos(ang);
        if (isChLen) {
            double d = Math.sqrt(vx * vx + vy * vy);
            vx = vx / d * newLen;
            vy = vy / d * newLen;
            param[0] = vx;
            param[1] = vy;
        }

        return param;
    }

    public static double lineSpace(int x1, int y1, int x2, int y2) {
        return Math.sqrt((double)((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)));
    }

    public static double pointToLine(int x1, int y1, int x2, int y2, int x0, int y0) {
        double a = lineSpace(x1, y1, x2, y2);
        double b = lineSpace(x1, y1, x0, y0);
        double c = lineSpace(x2, y2, x0, y0);
        double space;
        if (c > 1.0E-6D && b > 1.0E-6D) {
            if (a <= 1.0E-6D) {
                return b;
            } else if (c * c >= a * a + b * b) {
                return b;
            } else if (b * b >= a * a + c * c) {
                return c;
            } else {
                double p = (a + b + c) / 2.0D;
                double s = Math.sqrt(p * (p - a) * (p - b) * (p - c));
                space = 2.0D * s / a;
                return space;
            }
        } else {
            space = 0.0D;
            return space;
        }
    }

    public static int direction(Point p0, Point p1, Point p2) {
        return (p2.x - p0.x) * (p1.y - p0.y) - (p1.x - p0.x) * (p2.y - p0.y);
    }

    public static double mult(Point a, Point b, Point c) {
        return (double)((a.x - c.x) * (b.y - c.y) - (b.x - c.x) * (a.y - c.y));
    }

    public static boolean intersect1(Point aa, Point bb, Point cc, Point dd) {
        return Math.max(aa.x, bb.x) >= Math.min(cc.x, dd.x) && Math.max(aa.y, bb.y) >= Math.min(cc.y, dd.y) && Math.max(cc.x, dd.x) >= Math.min(aa.x, bb.x) && Math.max(cc.y, dd.y) >= Math.min(aa.y, bb.y) && mult(cc, bb, aa) * mult(bb, dd, aa) >= 0.0D && mult(aa, dd, cc) * mult(dd, bb, cc) >= 0.0D;
    }
}
